package com.luxoft.scoremanager.web;

import com.luxoft.scoremanager.server.model.bean.ComponentBean;
import com.luxoft.scoremanager.server.model.bean.ScorePackageBean;
import com.luxoft.scoremanager.server.model.bean.StaticDataBean;
import com.luxoft.scoremanager.server.service.PackageManagerService;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;


@Controller
@RequestMapping("/scoremanager")
public class PackageManagerController {
   /* @Autowired
    private PackageManagerService packageManagerService;*/

    @Resource(name = "repositoryPackageManagerService")
    private PackageManagerService packageManagerService;

    private static Logger LOG = Logger.getLogger(PackageManagerController.class);


    public void setPackageManagerService(PackageManagerService packageManagerService) {
        this.packageManagerService = packageManagerService;
    }

    @RequestMapping(value = "/packageId/{id}", method = RequestMethod.GET)
    public String handle1Request1(@PathVariable("id") Integer id) {
        LOG.trace("Get package with ID = " + id);
        return "index";
    }

    @Deprecated
    @RequestMapping(value = "/packageId/{id}/package/read", method = RequestMethod.GET)
    public
    @ResponseBody
    ScorePackageBean readPackage(@PathVariable("id") Integer id) {
        LOG.trace("Package: " + id + "  reading package data from JSON");
        return packageManagerService.getScorePackageById(id);
    }

    @Deprecated
    @RequestMapping(value = "/packageId/{id}/staticdata/read", method = RequestMethod.GET)
    public
    @ResponseBody
    List<StaticDataBean> readStaticData(@PathVariable("id") Integer id) {
        LOG.trace("Package: " + id + "  reading StaticData from JSON");
        return packageManagerService.getAllStaticData();
    }

    @Deprecated
    @RequestMapping(value = "/packageId/{id}/components/read", method = RequestMethod.GET)
    public
    @ResponseBody
    List<ComponentBean> readComponents(@PathVariable("id") Integer id) {
        LOG.trace("Package: " + id + "  reading Components from JSON");
        return packageManagerService.getAllComponentByIdOfScorePackage(id);
    }

    @Deprecated
    @RequestMapping(value = "/packageId/{id}/components/update", method = RequestMethod.POST)
    public
    @ResponseBody
    ComponentBean updateComponent(@PathVariable("id") Integer id, @RequestBody ComponentBean componentBean) {
        LOG.trace("Package: " + id + ";  updating component: " + componentBean);
        packageManagerService.updateComponentInScorePackageByPackageId(id, componentBean);
        return componentBean;
    }

    @Deprecated
    @RequestMapping(value = "/packageId/{id}/package/update", method = RequestMethod.POST)
    public
    @ResponseBody
    ScorePackageBean updatePackage(@PathVariable("id") Integer id, @RequestBody ScorePackageBean scorePackageBean) {
        LOG.trace("Package: " + id + ";  updating package: " + scorePackageBean);
        packageManagerService.updateScorePackage(scorePackageBean);
        return scorePackageBean;
    }

    @RequestMapping(value = "/packageId/{id}/fullpackage/read", method = RequestMethod.GET)
    public
    @ResponseBody
    ScorePackageBean readFullPackage(@PathVariable("id") Integer id) {
        ScorePackageBean scorePackageBean;
        scorePackageBean = packageManagerService.getScorePackageById(id);
        return scorePackageBean;
    }

    @RequestMapping(value = "/packageId/{id}/fullpackage/update", method = RequestMethod.POST)
    public
    @ResponseBody
    ScorePackageBean updateFullPackage(@PathVariable("id") Integer id, @RequestBody ScorePackageBean scorePackageBean) {
        LOG.trace(scorePackageBean);
        packageManagerService.updateScorePackage(scorePackageBean);
       /* RepositoryPackageManagerService packageManagerService = new RepositoryPackageManagerService();
        packageManagerService.update(scorePackageBean);*/
        /*repositoryPackageManagerService.update(scorePackageBean);*/
        return scorePackageBean;
    }
}
